<template>
  <div class="app-container">

    <el-dialog
      title="修改食品信息"
      :visible.sync="dialogVisible"
      width="30%"
      :before-close="handleClose"
    >
      <el-form :model="form">
        <el-form-item label="食品名称" prop="name">
          <el-input v-model="form.name" autocomplete="off" />
        </el-form-item>
        <el-form-item label="食品图片">
          <el-upload
            action="fakeAction"
            list-type="picture-card"
            :http-request="uploadFile"
            :limit="1"
          >
            <i class="el-icon-plus" />
          </el-upload>
        </el-form-item>
        <el-form-item label="热量" prop="size">
          <el-input v-model="form.size" autocomplete="off" />
        </el-form-item>
        <el-form-item label="简略备注" prop="remark">
          <el-input v-model="form.remark" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="原文链接" prop="remarkUrl">
          <el-input v-model="form.remarkUrl" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="doUpdate(form)">确 定</el-button>
      </div>
    </el-dialog>
    <el-table
      v-loading="listLoading"
      :data="foodList"
      element-loading-text="Loading"
      border
      fit
      highlight-current-row
    >
      <el-table-column
        type="selection"
        width="55"
      />
      <el-table-column align="center" label="ID" width="95" prop="id" />
      <el-table-column label="品名" prop="name" width="140" />
      <el-table-column label="图片" width="160" align="center">
        <template slot-scope="scope">
          <img :src="scope.row.pic" alt="" style="height: 80px" width="160px">
        </template>
      </el-table-column>
      <el-table-column label="味道" width="140" align="center" prop="smell" />
      <el-table-column label="热量" width="140" align="center" prop="size" />
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button
            size="middle"
            @click="handleUpdate(scope.row)"
          >编辑
          </el-button>
          <el-button
            size="middle"
            type="danger"
            @click="handleDelete(scope.row)"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>
<script>
import { getFoodList } from '@/api/food'
import { deleteFood } from '@/api/food'
import { uploadFiles } from '@/api/common'
import { updateFood } from '@/api/food'

export default {
  data() {
    return {
      form: {
        name: '',
        pic: '',
        size: '',
        remark: '',
        remarkUrl: ''
      },
      param: {},
      foodList: [],
      listLoading: true,
      dialogVisible: false
    }
  },

  created() {
    this.feachDate()
  },
  methods: {
    onSubmit() {
      this.$message('submit!')
    },
    onCancel() {
      this.$message({
        message: 'cancel!',
        type: 'warning'
      })
    },

    feachDate() {
      getFoodList(this.param).then(res => {
        this.foodList = res.data
        this.listLoading = false
      })
    },
    handleDelete(data) {
      let {$message} = this
      deleteFood(data).then(res => {
        console.log(data, res)
        if (res.code === 100) {
          $message = '删除成功'
          this.feachDate()
        }
      })
    },
    handleUpdate(data) {
      this.form = data
      this.dialogVisible = true
    },
    uploadFile(file) {
      // console.log(111)
      // 根据后台需求数据格式
      const form = new FormData()
      // 文件对象
      form.append('file', file.file)
      // 要在请求时添加特定属性（其他参数），所以要用自己方法覆盖默认的action
      // form.append('hld', JSON.stringify(this.uploadForm))
      uploadFiles(form).then((res) => {
        if (res) {
          this.$message.success('图片上传成功')
          console.log(res)
          this.form.pic = res.data.webUrl
        }
        // 上传完成后，清空缓存
        // this.$refs.upload.clearFiles()
        // this.$refs.uploadForm.resetFields()
      }).catch((error) => {
        console.log('上传失败 file：', file)
        // 即使失败，也会清除上传的图片，因为只有上传图片的动作发生时，他才会调用上传接口
        this.$refs.upload.clearFiles()
        this.$message.warning(error.message)
      })
    },
    doUpdate(data) {
      updateFood(data).then(res => {
        console.log(res)
        if (res.code === 100) {
          this.$message = '修改成功'
          this.dialogVisible = false
        }
      })
    }

  }
}
</script>

<style scoped>
  .line {
    text-align: center;
  }
</style>

